<template>
  <div class="coupon-page" v-loading="loading" element-loading-text="加载中...">
    <div class="coupon-title">
      <div class="coupon-title-left">
        <!-- <el-form inline>
            <el-form-item>
              <el-select v-model="searchForm.status" clearable placeholder="状态">
                <el-option label="有效" :value="1" />
                <el-option label="无效" :value="0" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="searchFun">查询</el-button>
              <el-button @click="resetFun">重置</el-button>
            </el-form-item>
          </el-form> -->
        <span style="vertical-align: middle"> 优惠券</span>
      </div>
      <div class="coupon-title-right">
        <el-button
          v-if="showItem"
          type="primary"
          :icon="Plus"
          @click="addDeptFun('add')"
        >
          添加
        </el-button>
      </div>
    </div>
    <div class="coupon-table">
      <el-table
        border
        stripe
        :data="tableData"
        :height="store.tableHeight - 120"
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column prop="coupon_title" label="优惠券名称" />
        <el-table-column prop="created_at" label="创建时间" width="200">
        </el-table-column>
        <el-table-column prop="coupon_price" label="优惠券面值" width="100">
        </el-table-column>

        <!-- <el-table-column prop="start_time" label="领取开启时间" width="170" />
                <el-table-column prop="end_time" label="领取结束时间" width="170" /> -->
        <!-- 
                <el-table-column prop="status" label="状态" width="130">
                    <template #default="{ row }">
                        <el-tag v-if="row.status == 0">未使用</el-tag>
                        <el-tag v-else-if="row.status == 1" type="success">
                            已使用
                        </el-tag>
                        <el-tag v-else-if="row.status == 2" type="danger">
                            已过期
                        </el-tag>
                    </template>
                </el-table-column> -->

        <el-table-column prop="status" label="启用/禁用" width="90">
          <template #default="{ row }">
            <el-switch
              v-model="row.status"
              active-text="有效"
              :active-value="1"
              inactive-text="无效"
              :inactive-value="0"
              inline-prompt
              @change="statusFun(row)"
            />
          </template>
        </el-table-column>

        <el-table-column label="操作" width="120" align="center">
          <template #default="{ row }">
            <!-- <el-button type="primary" link @click="updateOpen(row)">编辑</el-button> -->

            <el-button type="danger" link @click="delFun(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-div">
        <div>
          <!-- <el-button type="danger" :disabled="selectData.length == 0 ? true : false" @click="delAllFun">
                        <template #icon>
                            <IEpDelete />
                        </template>
                        批量删除</el-button> -->
        </div>
        <el-pagination
          v-model:currentPage="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[20, 50, 100, 200]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
        <div></div>
      </div>
    </div>
  </div>
  <addDialog
    :dataInfo="dataInfo"
    ref="addDialogRef"
    @refreshList="refreshList"
  />
</template>
<script setup>
import { Plus } from "@element-plus/icons-vue";
import { useCounterStore } from "@/stores/counter.js";
import { listApi, delApi, stateApi } from "@/plugins/api/customer/manage/tab7";
import addDialog from "./DialogTab7.vue";
const props = defineProps({
  dataInfo: {
    type: Object,
    default: {},
  },
  customInfo: {
    type: Object,
    default: {},
  },
  showItem: {
    type: Boolean,
    default: false,
  },
});
const store = useCounterStore();
const router = useRouter();
let searchForm = ref({});
function searchFun() {
  selectData.value = [];
  tableData.value = [];
  getData();
}
function resetFun() {
  searchForm.value = {};
  tableData.value = [];
  selectData.value = [];
  getData();
}
let tableData = ref([]);
let loading = ref(false);
const currentPage = ref(1);
const pageSize = ref(20);
const total = ref(0);
const handleSizeChange = (val) => {
  pageSize.value = val;
  getData();
};
const handleCurrentChange = (val) => {
  currentPage.value = val;
  getData();
};
function statusFun(row) {
  stateApi({
    status: row.status,
    id: row.id,
  }).then(() => {
    ElMessage({
      type: "success",
      message: "修改成功！",
    });
    getData();
  });
}
function getData() {
  loading.value = true;
  let obj = Object.assign(
    {
      page: currentPage.value,
      pageSize: pageSize.value,
      customer_id: props.dataInfo.id,
    },
    searchForm.value
  );
  listApi(obj)
    .then((data) => {
      tableData.value = data.list;
      total.value = data.page.total;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}
let addDialogRef = ref(null);
function addDeptFun(type) {
  addDialogRef.value.initFun(type);
}
function updateOpen(row) {
  addDialogRef.value.initFun("update", row);
}
function refreshList() {
  getData();
}
let selectData = ref([]);
function handleSelectionChange(val) {
  selectData.value = val;
}
function delFun(row) {
  ElMessageBox.confirm("确认删除，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi({
        id: row.id.toString(),
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}

function delAllFun() {
  ElMessageBox.confirm("确认批量删除选中的数据，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      let arr = selectData.value.map((ele) => {
        return ele.id;
      });
      delApi({
        id: arr.join(","),
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
        selectData.value = [];
        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}

function initFun() {
  currentPage.value = 1;
  getData();
}
defineExpose({
  initFun,
});

onMounted(() => {
  getData();
});
</script>
<style scoped lang="scss">
.coupon-page {
  width: 100%;
  height: 100%;

  .coupon-title {
    width: 100%;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #eeeeee;
    padding-bottom: 10px;

    .coupon-title-left {
      font-size: 18px;
      font-weight: 700;
      color: #172b4d;

      :deep(.el-form-item) {
        margin-bottom: 0;
      }
    }
  }

  .coupon-table {
    padding: 10px;
    box-sizing: border-box;

    .pagination-div {
      display: flex;
      justify-content: space-between;
      margin-top: 10px;
    }
  }
}
</style>
